<template>
  <div class="app-container">

    <!-- 查询表单 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="userQuery.name" placeholder="用户名" />
      </el-form-item>

      <el-form-item>
        <el-date-picker
          v-model="userQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="userQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table :data="list" border fit highlight-current-row>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="username" label="用户名" width="80" />

      <el-table-column prop="othername" label="别名" width="80" />

      <el-table-column prop="sex" label="性别" width="60">
          <template slot-scope="scope">
              {{scope.row.sex == 1 ? '男' : '女'}}
          </template>
      </el-table-column>

      <el-table-column label="用户类型" width="80" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 0" >管理员</el-tag>
          <el-tag v-else-if="scope.row.type == 1" type="success">学生</el-tag>
          <el-tag v-else-if="scope.row.type == 2" type="info">教师</el-tag>
          <el-tag v-else-if="scope.row.type === 3" type="warning">企业</el-tag>
          <!-- {{ scope.row.type==0?'管理员':'首席讲师' }} -->
        </template>
      </el-table-column>

      <el-table-column prop="email" label="邮箱" />

      <el-table-column prop="mobilephone" label="手机" />

      <el-table-column prop="department" label="系别" width="160" />

      <el-table-column prop="grade" label="班级" width="160" />

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/user/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button
            >
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteUserById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>

<script>
import userApi from "@/api/user";

export default {
  data() {
    return {
      list: null,
      page: 1, //当前页
      limit: 10, //每页记录数
      total: 0, //总记录数
      userQuery: {}, //条件封装对象
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList(page = 1) {
      this.page = page;
      userApi
        .getUserList(this.page, this.limit, this.userQuery)
        .then((response) => {
          //请求成功
          //response接口返回的数据
          this.list = response.data.items;
          this.total = response.data.total;
          console.log(this.list);
          console.log(this.total);
        });
    },
    resetData() {
      //表单输入项数据清空
      this.userQuery = {};
      //查询所有讲师数据
      this.getList();
    },
    deleteUserById(id) {
      this.$confirm("此操作将删除用户记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //点击确定，执行then方法
        //调用删除的方法
        userApi.deleteUserId(id).then((response) => {
          //删除成功
          //提示信息
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          //回到列表页面
          this.getList();
        });
      }); //点击取消，执行catch方法
    },
  },
};
</script>

<style>
</style>